package com.csu.tingtian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.entity.ProduceLineItem;
import com.csu.tingtian.entity.ProduceOrders;
import com.csu.tingtian.entity.ProduceOrderstatus;
import com.csu.tingtian.persistence.ProduceLineItemMapper;
import com.csu.tingtian.persistence.ProduceOrdersMapper;
import com.csu.tingtian.persistence.ProduceOrderstatusMapper;
import com.csu.tingtian.service.ProduceOrderService;
import com.csu.tingtian.vo.ProduceOrder;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("ProduceOrderService")
public class ProduceOrderImpl implements ProduceOrderService {
    @Autowired
    ProduceOrdersMapper produceOrdersMapper;
    @Autowired
    ProduceOrderstatusMapper produceOrderstatusMapper;
    @Autowired
    ProduceLineItemMapper produceLineItemMapper;

    @Override
    public CommonResponse<List<ProduceOrder>> getAllProduceOrder() {
        List<ProduceOrder> produceOrderList = new ArrayList<>();
        List<ProduceOrders> produceOrdersList = produceOrdersMapper.selectList(null);
        List<ProduceOrderstatus> produceOrderstatusesList = produceOrderstatusMapper.selectList(null);

        LoggerFactory.getLogger(ProduceOrderImpl.class).info("你爸爸来请求你了");

        if (produceOrdersList != null) {
            for (int i = 0; i < produceOrdersList.size(); i++) {
                ProduceOrder produceOrder = new ProduceOrder();
                int orderid = produceOrdersList.get(i).getOrderId();
                produceOrder.setOrderId(orderid);
                produceOrder.setUserName(produceOrdersList.get(i).getUserName());
                produceOrder.setOrderCreateDate(produceOrdersList.get(i).getOrderCreateDate());
                produceOrder.setDeliveryName(produceOrdersList.get(i).getDeliveryName());
                produceOrder.setDeliveryPhoneNum(produceOrdersList.get(i).getDeliveryPhoneNum());
                produceOrder.setDeliveryAddress(produceOrdersList.get(i).getDeliveryAddress());
                produceOrder.setTotalPrice(produceOrdersList.get(i).getTotalPrice());
                produceOrder.setPaymentStatus(produceOrdersList.get(i).getPaymentStatus());
                produceOrder.setPaidDate(produceOrdersList.get(i).getPaidDate());
                produceOrder.setOrderStatus(produceOrderstatusesList.get(i).getOrderStatus());

                QueryWrapper<ProduceLineItem> queryWrapper = new QueryWrapper<>();
                produceOrder.setProduceLineItemList(produceLineItemMapper.selectList(queryWrapper.eq("orderId",orderid)));
                produceOrderList.add(produceOrder);
            }
            return CommonResponse.createForSuccess(produceOrderList);
        }
        else
            return CommonResponse.createForError("数据库中找不到农产品订单信息");
    }

    @Override
    public CommonResponse<List<ProduceOrder>> getProduceOrderByUserName(String userName) {
        List<ProduceOrder> produceOrderList = new ArrayList<>();
        QueryWrapper<ProduceOrders> produceOrdersQueryWrapper = new QueryWrapper<>();
        List<ProduceOrders> produceOrdersList = produceOrdersMapper.selectList(produceOrdersQueryWrapper.eq("userName",userName));

        if (produceOrdersList != null) {
            for (int i = 0; i < produceOrdersList.size(); i++) {
                ProduceOrder produceOrder = new ProduceOrder();
                produceOrder.setOrderId(produceOrdersList.get(i).getOrderId());
                produceOrder.setUserName(produceOrdersList.get(i).getUserName());
                produceOrder.setOrderCreateDate(produceOrdersList.get(i).getOrderCreateDate());
                produceOrder.setDeliveryName(produceOrdersList.get(i).getDeliveryName());
                produceOrder.setDeliveryPhoneNum(produceOrdersList.get(i).getDeliveryPhoneNum());
                produceOrder.setDeliveryAddress(produceOrdersList.get(i).getDeliveryAddress());
                produceOrder.setTotalPrice(produceOrdersList.get(i).getTotalPrice());
                produceOrder.setPaymentStatus(produceOrdersList.get(i).getPaymentStatus());
                produceOrder.setPaidDate(produceOrdersList.get(i).getPaidDate());

                int orderid = produceOrdersList.get(i).getOrderId();
                ProduceOrderstatus produceOrderstatus = produceOrderstatusMapper.selectById(orderid);
                produceOrder.setOrderStatus(produceOrderstatus.getOrderStatus());

                QueryWrapper<ProduceLineItem> queryWrapper = new QueryWrapper<>();
                produceOrder.setProduceLineItemList(produceLineItemMapper.selectList(queryWrapper.eq("orderId",orderid)));


                produceOrderList.add(produceOrder);
            }
            return CommonResponse.createForSuccess(produceOrderList);
        }
        else
            return CommonResponse.createForError("数据库中找不到用户" + userName +"的农产品订单信息");
    }

    @Override
    public CommonResponse<ProduceOrder> getProduceOrderByOrderId(int orderId) {
        ProduceOrder produceOrder = new ProduceOrder();
        ProduceOrders produceOrders = produceOrdersMapper.selectById(orderId);
        if (produceOrders != null) {
            ProduceOrderstatus produceOrderstatus = produceOrderstatusMapper.selectById(orderId);
            produceOrder.setOrderId(produceOrders.getOrderId());
            produceOrder.setUserName(produceOrders.getUserName());
            produceOrder.setOrderCreateDate(produceOrders.getOrderCreateDate());
            produceOrder.setDeliveryName(produceOrders.getDeliveryName());
            produceOrder.setDeliveryPhoneNum(produceOrders.getDeliveryPhoneNum());
            produceOrder.setDeliveryAddress(produceOrders.getDeliveryAddress());
            produceOrder.setTotalPrice(produceOrders.getTotalPrice());
            produceOrder.setPaymentStatus(produceOrders.getPaymentStatus());
            produceOrder.setPaidDate(getSystemTime());
            produceOrder.setOrderStatus(produceOrderstatus.getOrderStatus());

            QueryWrapper<ProduceLineItem> queryWrapper = new QueryWrapper<>();
            produceOrder.setProduceLineItemList(produceLineItemMapper.selectList(queryWrapper.eq("orderId",orderId)));

            return CommonResponse.createForSuccess(produceOrder);
        }
        else
            return CommonResponse.createForError("数据库中没有订单号为:" + orderId + "的订单");
    }

    @Override
    public CommonResponse<ProduceOrder> insertProduceOrder(ProduceOrder produceOrder){
        ProduceOrders produceOrders = new ProduceOrders();
        ProduceOrderstatus produceOrderstatus = new ProduceOrderstatus();
        int orderid = getNextOrderId();

        produceOrders.setOrderId(orderid);
        produceOrders.setUserName(produceOrder.getUserName());
//        System.out.println("xwz，当前系统时间是：" + getSystemTime());
        produceOrders.setOrderCreateDate(getSystemTime());
        produceOrders.setDeliveryName(produceOrder.getDeliveryName());
        produceOrders.setDeliveryPhoneNum(produceOrder.getDeliveryPhoneNum());
        produceOrders.setDeliveryAddress(produceOrder.getDeliveryAddress());
        produceOrders.setTotalPrice(produceOrder.getTotalPrice());
        produceOrders.setPaymentStatus(produceOrder.getPaymentStatus());
        produceOrders.setPaidDate(produceOrder.getPaidDate());

        produceOrderstatus.setOrderId(orderid);
        produceOrderstatus.setOrderStatus(produceOrder.getOrderStatus());


        int theNumOfLineItem = produceOrder.getProduceLineItemList().size();
        for (int i = 0; i < theNumOfLineItem; i++) {
            ProduceLineItem produceLineItem = new ProduceLineItem();
            produceLineItem.setOrderId(orderid);
            produceLineItem.setFarmName(produceOrder.getProduceLineItemList().get(i).getFarmName());
            produceLineItem.setItemId(produceOrder.getProduceLineItemList().get(i).getItemId());
            produceLineItem.setQuantity(produceOrder.getProduceLineItemList().get(i).getQuantity());
            produceLineItem.setUnitprice(produceOrder.getProduceLineItemList().get(i).getUnitprice());
            produceLineItemMapper.insert(produceLineItem);
        }
        //插入
        produceOrdersMapper.insert(produceOrders);
        produceOrderstatusMapper.insert(produceOrderstatus);




        if (getProduceOrderByOrderId(getNextOrderId()) != null)
            return CommonResponse.createForSuccess("插入成功",produceOrder);
        else
            return CommonResponse.createForError("服务器异常，插入订单失败。");

    }
    private int getNextOrderId(){
        int orderid = produceOrdersMapper.selectCount(null);
        orderid += 1;
        return orderid;
    }
    private String getSystemTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time= sdf.format( new Date());
        return time;
    }
    @Override
    public CommonResponse<ProduceOrder> deleteProduceOrderByOrderId(int orderId) {
        ProduceOrder produceOrder = getProduceOrderByOrderId(orderId).getData();
        //删除produceorders 表和produceorderstatus表中的内容
        int i = produceOrdersMapper.deleteById(orderId);
        produceOrderstatusMapper.deleteById(orderId);
        //删除ProduceLineItem中的内容
        QueryWrapper<ProduceLineItem> queryWrapper = new QueryWrapper<>();
        produceLineItemMapper.delete(queryWrapper.eq("orderId",orderId));

        if (i > -1)
            return CommonResponse.createForSuccess("删除订单"+ orderId+ "i为："+ i + "成功！", produceOrder);
        else
            return CommonResponse.createForError("服务器异常，订单删除失败。");
    }

    @Override
    public CommonResponse<ProduceOrder> updateProduceOrder(ProduceOrder produceOrder) {
        ProduceOrders produceOrders = new ProduceOrders();
        ProduceOrderstatus produceOrderstatus = new ProduceOrderstatus();
        int orderid = produceOrder.getOrderId();
        produceOrders.setOrderId(orderid);
        produceOrders.setUserName(produceOrder.getUserName());
        produceOrders.setOrderCreateDate(getSystemTime());
        produceOrders.setDeliveryName(produceOrder.getDeliveryName());
        produceOrders.setDeliveryPhoneNum(produceOrder.getDeliveryPhoneNum());
        produceOrders.setDeliveryAddress(produceOrder.getDeliveryAddress());
        produceOrders.setTotalPrice(produceOrder.getTotalPrice());
        produceOrders.setPaymentStatus(produceOrder.getPaymentStatus());
        produceOrders.setPaidDate(produceOrder.getPaidDate());


        produceOrderstatus.setOrderId(produceOrder.getOrderId());
        produceOrderstatus.setOrderStatus(produceOrder.getOrderStatus());

        int theNumOfLineItem = produceOrder.getProduceLineItemList().size();
        try{for (int i = 0; i < theNumOfLineItem; i++) {
            ProduceLineItem produceLineItem = new ProduceLineItem();
            produceLineItem.setOrderId(orderid);
            produceLineItem.setFarmName(produceOrder.getProduceLineItemList().get(i).getFarmName());
            produceLineItem.setItemId(produceOrder.getProduceLineItemList().get(i).getItemId());
            produceLineItem.setQuantity(produceOrder.getProduceLineItemList().get(i).getQuantity());
            produceLineItem.setUnitprice(produceOrder.getProduceLineItemList().get(i).getUnitprice());
            QueryWrapper<ProduceLineItem> queryWrapper = new QueryWrapper<>();
            produceLineItemMapper.update(produceLineItem,queryWrapper.eq("orderId",orderid));

        }}catch (Exception e){
            e.printStackTrace();
        }

        int j = produceOrdersMapper.updateById(produceOrders);
        produceOrderstatusMapper.updateById(produceOrderstatus);
        if (j > -1) //更新成功为0，失败为-1
            return CommonResponse.createForSuccess("修改订单成功",produceOrder);
        else
            return CommonResponse.createForError("服务器异常，修改订单失败。");
    }
}
